package com.ghh.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ghh.constant.MessageConstant;
import com.ghh.dao.CheckitemDao;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.pojo.CheckItem;
import com.ghh.service.CheckitemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class CheckitemServiceImpl implements CheckitemService {

    @Autowired
private CheckitemDao checkitemDao;

    //添加检查项
    @Override
    public void addCheckitem(CheckItem checkItem) {
        checkitemDao.addCheckitem(checkItem);
    }

    //分页查询检查项
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //定义需要的数据
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        //分页插件，会在执行sql之前将分页关键字追加到SQL后面
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = checkitemDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //删除检查项
    @Override
    public void deleteCheckitem(int id) {
        //判断当前检查项是否关联检查组
        long num = checkitemDao.findCountByCheckitem(id);
        if(num > 0){
            //已经被关联，不能删除
            throw new RuntimeException(MessageConstant.DELETE_CHECKITEM_FAIL);
        }else{
            //可以删除
            checkitemDao.deleteCheckitem(id);
        }
    }

    //修改检查项
    @Override
    public void updateCheckitem(CheckItem checkItem) {
        checkitemDao.updateCheckitem(checkItem);
    }

    //根据ID查找检查项
    @Override
    public CheckItem findById(int id) {
        return checkitemDao.findById(id);
    }

    //查询所有检查项
    @Override
    public List<CheckItem> findAll() {
        return checkitemDao.findAll();
    }
}
